package com.huahua.api.controller.common;

import com.huahua.api.entity.base.QiNiuOssUploadBean;
import com.huahua.api.entity.base.R;
import com.huahua.api.util.QiNiuUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * @author LoveHuaHua
 * @date 2023年08月22日 12:40
 * @description believe in yourself
 */

@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class OssController {
    private final QiNiuUtils ossUtil;

    /**
     * 获取七牛云上传 token
     *
     * @return {@link Object}
     */
    @GetMapping("/getUpToken")
    public Mono<R<String>> getUpToken() {
        return Mono.just(R.success(ossUtil.getUpToken()));
    }


    @PostMapping("/uploadTemp")
    public Mono<R<String>> uploadTempImg(@RequestParam(value = "path", defaultValue = "temp") String path, @RequestPart("file") Mono<FilePart> filePart) {
        return filePart.flatMap(file -> {
            QiNiuOssUploadBean qiNiuOssUploadBean = ossUtil.uploadFilePart(file, path);
            return Mono.just(R.success(qiNiuOssUploadBean.getUrl()));
        });
    }
}
